﻿import mx.utils.Delegate;
import flash.filters.DropShadowFilter;
// import Tweener Class
import caurina.transitions.Tweener;
import caurina.transitions.Equations;

class XMLContentLoader
{
	public var st_arr : Array = new Array ();
	//total Array
	public var idArray : Array = new Array ();
	//array only for ID nav1, nav2,nav3... ect
	private var _xml : XML;
	public var positionArray : Array = new Array();
	public var highlightArray : Array = new Array();
	public var contentArray : Array = new Array();
	public var footerArray : Array = new Array();
	
	public var thumbnailArray : Array = new Array ();
	public var shorttextArray : Array = new Array();
	public var linkArray : Array = new Array();
	public var filterArray:Array = new Array();
	public var effectArray:Array = new Array();
	
	public var topLevel : Object;
	var image_mcl : MovieClipLoader;
	var mclListener : Object = new Object ();
	
	function XMLContentLoader (url : String, _topLevel : Object)
	{
		topLevel = _topLevel;
		
		// MovieClipLoader and 3 event		
		image_mcl = new MovieClipLoader ();
		mclListener.onLoadProgress = function(target_mc : MovieClip, bytesLoaded:Number, bytesTotal:Number):Void {
			//trace(target_mc + ".onLoadProgress with " + bytesLoaded + " bytes of " + bytesTotal);
		}

		mclListener.onLoadComplete = function (target_mc : MovieClip)
		{
			//trace ("onLoadComplete");			
		};
		mclListener.onLoadInit = function (target_mc : MovieClip)
		{
			//trace (target_mc + ".onLoadInit");	

		};
		image_mcl.addListener (mclListener);
		//MovieClipLoader
		
		
		_xml = new XML ();
		_xml.ignoreWhite = true;
		_xml.onLoad = Delegate.create (this, onLoadEvent);
		/*
		_xml.onLoad = function (success)
		{
		if (success)
		st_arr = this.firstChild.childNodes;
		trace ('st_arr: ' + st_arr.length);
		}
		*/
		_xml.load (url);
	}
	function onLoadEvent (success : Boolean) : Void
	{
		if (success)
		{
			st_arr = _xml.firstChild.childNodes;
			//output (st_arr);
			generateIDArray (st_arr);
			generatePositionArray(st_arr);
			generateHighlightArray(st_arr);
			generateContentArray(st_arr);
			generateFooterArray(st_arr);
			generateShorttextArray(st_arr);
			generateLinkArray(st_arr);
			generateEffectArray(st_arr);
			dropShadowEffect();
			initBGBoard ();
			initThumbnail ();
			topLevel.icons = thumbnailArray;
			topLevel.init ();
		}
	}
	/**
	* generate ID Array
	*
	* @usage array only for ID like nav1, nav2,nav3... ect
	* @param
	*/
	function generateIDArray (_idArray : Array)
	{
		for (var i = 0; i < _idArray.length; i ++)
		{
			idArray [i] = _idArray [i].attributes.id
			idArray [i] = "nav" + idArray [i];
			trace ('idArray [i]: ' + idArray [i]);
		}
	}
	function output (_array : Array)
	{
		trace ("output");
		trace (_xml.firstChild.nodeName + ":" + _xml.firstChild.attributes.pic_path);
		//st_arr = _xml.firstChild.childNodes;
		for (var i = 0; i < st_arr.length; i ++)
		{
			trace ("=======evert selection==============");
			trace (st_arr [i].nodeName + " : " + st_arr [i].attributes.id);
			//var child_arr:Array = st_arr[i].firstChild.childNodes;
			for (var j = 0; j < st_arr [i].childNodes.length; j ++)
			{
				trace (st_arr [i].childNodes [j].nodeName + " : " + st_arr [i].childNodes [j].firstChild.nodeValue);
				// print text
				if (j == 1)
				{
					for (var k = 0; k < st_arr [i].childNodes [1].childNodes.length; k ++)
					{
						trace (st_arr [i].childNodes [1].childNodes [k].nodeName + " : " + st_arr [i].childNodes [1].childNodes [k].firstChild.nodeValue);
					}
				}
				//print thumbnail
				if (j == 2)
				{
					for (var k = 0; k < st_arr [i].childNodes [2].childNodes.length; k ++)
					{
						trace (st_arr [i].childNodes [2].childNodes [k].nodeName + " : " + st_arr [i].childNodes [2].childNodes [k].firstChild.nodeValue);
					}
				}
			}
		}
	}
	//load_BGBoard
	function initBGBoard () : Void
	{
		for (var i = 0; i < st_arr.length; i ++)
		{
			var _picpath : String = _xml.firstChild.attributes.pic_path + "/" + st_arr [i].childNodes [0].firstChild.nodeValue;
			trace (_picpath);
			var container : MovieClip = _root.BGBoard.createEmptyMovieClip ("bg_mc" + i, _root.BGBoard.getNextHighestDepth ());
			//container.loadMovie(_picpath);
			image_mcl.loadClip (_picpath, container);
			container._y = Stage.height * i;
			trace (container);
		}
	}
	//load_BGBoard
	function load_BGBoard (_bgid : Number)
	{
		Tweener.addTween (_root.BGBoard, {_y : _y - Stage.height * _bgid, time : 1});
		/*
		_root.BGBoard.onEnterFrame = function(){
		this._y +=( Stage.height * _bgid-this._y)/10;
		}
		*/
	}
	function load_thumbnail (_bgid : Number) : String
	{
		var _picpath : String = _xml.firstChild.attributes.pic_path + "/" + st_arr [_bgid].childNodes [2].firstChild.firstChild.nodeValue;
		trace (_picpath);
		return _picpath;
	}
	// Initializes the thumbnails
	function initThumbnail ()
	{
		for (var i = 0; i < st_arr.length; i ++)
		{
			var container : MovieClip = _root.createEmptyMovieClip ("nav" + i, _root.getNextHighestDepth ());
			container.loadMovie (load_thumbnail (i));
			container._x = i * 100;
			trace (container);
			thumbnailArray.push (container);
		}
	}
	
	//position
	function generatePositionArray(_array){
		for (var i = 0; i < _array.length; i ++)
		{
			positionArray [i] = _array [i].childNodes [1].firstChild.childNodes [0].nodeValue;
			trace ('positionArray [i]: ' + positionArray [i]);
		}
	}
	
	//highlight
	function generateHighlightArray(_array){
		for (var i = 0; i < _array.length; i ++)
		{
			highlightArray [i] = _array [i].childNodes [1].childNodes [1].firstChild.nodeValue;
			trace ('highlightArray [i]: ' + highlightArray [i]);
		}
		var container:MovieClip = _root.context.createEmptyMovieClip("highlight", _root.context.getNextHighestDepth());
		container._x = 6;
		container._y = 0;
		var label:TextField = container.createTextField("label", 1, 0, 0, 300, 30);
		container.label.autoSize = "left";
		container.label.html = true;
		container.label.selectable = false;
		container.label.antiAliasType = "advanced";
	}
	
	//contentArray
	function generateContentArray(_array){
		for (var i = 0; i < _array.length; i ++)
		{
			contentArray [i] = _array [i].childNodes [1].childNodes [2].firstChild.nodeValue;
			trace ('contentArray [i]: ' + contentArray [i]);
		}
		var container:MovieClip = _root.context.createEmptyMovieClip("content", _root.context.getNextHighestDepth());
		container._x = 6;
		container._y = 30;
		var label:TextField = container.createTextField("label", 1, 0, 0, 300, 60);
		container.label.wordWrap = true;
		container.label.html = true;
		container.label.selectable = false;
		container.label.antiAliasType = "advanced";		
	}
	
	//footerArray
	function generateFooterArray(_array){
		for (var i = 0; i < _array.length; i ++)
		{
			footerArray [i] = _array [i].childNodes [1].childNodes [3].firstChild.nodeValue;
			trace ('footerArray [i]: ' + footerArray [i]);
		}
		var container:MovieClip = _root.context.createEmptyMovieClip("footer", _root.context.getNextHighestDepth());
		container._x = 6;
		container._y = 90;
		var label:TextField = container.createTextField("label", 1, 0, 0, 300, 20);
		container.label.autoSize = "left";
		container.label.html = true;
		container.label.selectable = false;
		container.label.antiAliasType = "advanced";
	}
	//effectArray
	function generateEffectArray(_array){
		for (var i = 0; i < _array.length; i ++)
		{
			effectArray [i] = _array [i].childNodes [1].childNodes [4].firstChild.nodeValue;
			trace ('effectArray [i]: ' + effectArray [i]);
		}
	}

	//shorttextArray
	function generateShorttextArray(_array){
		for (var i = 0; i < _array.length; i ++)
		{
			shorttextArray [i] = _array [i].childNodes [2].childNodes [1].firstChild.nodeValue;
			trace ('shorttextArray [i]: ' + shorttextArray [i]);
		}
		var container:MovieClip = _root.createEmptyMovieClip("shorttext", _root.context.getNextHighestDepth());
		var label:TextField = container.createTextField("label", 1, 0, 0, 200, 20);
		container.label.autoSize = "left";
		container.label.html = true;
		container.label.antiAliasType = "advanced";
	}
	
	//linkArray
	function generateLinkArray(_array){
		for (var i = 0; i < _array.length; i ++)
		{
			linkArray [i] = _array [i].childNodes [3].firstChild.nodeValue;
			trace ('linkArray [i]: ' + linkArray [i]);
		}
	}
	
	function dropShadowEffect():Void{
		trace("DropShadowFilter");
		var distance:Number = 1;
		var angleInDegrees:Number = 45;
		var color:Number = 0x000000;
		var alpha:Number = 0.5;
		var blurX:Number = 2;
		var blurY:Number = 2;
		var strength:Number = 1;
		var quality:Number = 1;
		var inner:Boolean = false;
		var knockout:Boolean = false;
		var hideObject:Boolean = false;
		// a DropShadowFilter
		
		var filter:DropShadowFilter;
		filter = new DropShadowFilter(distance, angleInDegrees, color, alpha, blurX, blurY, strength, quality, inner, knockout, hideObject);
		//var _filterArray:Array = new Array();
		filterArray.push(filter);
	}
	

}
